package org.leafstar.shortlink.admin.controller;

import lombok.RequiredArgsConstructor;
import org.leafstar.shortlink.admin.common.convention.result.Result;
import org.leafstar.shortlink.admin.common.convention.result.Results;
import org.leafstar.shortlink.admin.dto.req.UserLoginReqDTO;
import org.leafstar.shortlink.admin.dto.req.UserRegisterReqDTO;
import org.leafstar.shortlink.admin.dto.req.UserUpdateReqDTO;
import org.leafstar.shortlink.admin.dto.resp.UserActualRespDTO;
import org.leafstar.shortlink.admin.dto.resp.UserLoginRespDTO;
import org.leafstar.shortlink.admin.dto.resp.UserRespDTO;
import org.leafstar.shortlink.admin.service.UserService;
import org.leafstar.shortlink.admin.util.BeanUtil;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/short-link/admin/v1/user")
public class UserController {
    private final UserService userService;

    /**
     * 通过用户名获得用户
     */
    @GetMapping("/{username}")
    public Result<UserRespDTO> getUserByUserName(@PathVariable("username") String username) {
        return Results.success(userService.getUserByName(username));
    }

    /**
     * 获得用户的真实信息
     *
     * @param username 用户名
     * @return 用户的真实信息
     */
    @GetMapping("/actual/{username}")
    public Result<UserActualRespDTO> getActualUserByUserName(@PathVariable("username") String username) {
        return Results.success(BeanUtil.convert(userService.getUserByName(username), new UserActualRespDTO()));
    }

    /**
     * 判断用户名是否存在
     *
     * @param username 用户名
     * @return 是否存在
     */
    @GetMapping("/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username) {
        return Results.success(userService.hasUsername(username));
    }

    /**
     * 注册用户
     *
     * @param registerReqDTO 注册请求参数
     * @return 是否成功
     */
    @PostMapping("")
    public Result<Void> registerUser(@RequestBody UserRegisterReqDTO registerReqDTO) {
        userService.Register(registerReqDTO);
        return Results.success();
    }

    /**
     * 通过用户名修改用户
     * 通过RESTFUL风格而不写路由地址
     */
    @PutMapping("")
    public Result<Void> updateUser(@RequestBody UserUpdateReqDTO userUpdateReqDTO) {
        userService.updateUser(userUpdateReqDTO);
        return Results.success();
    }

    /**
     * 用户登录
     * @param userLoginReqDTO 登陆参数
     * @return 登陆结果
     */
    @PostMapping("/login")
    public Result<UserLoginRespDTO> userLogin(@RequestBody UserLoginReqDTO userLoginReqDTO) {
        UserLoginRespDTO res = userService.userLogin(userLoginReqDTO);
        return Results.success(res);
    }

    /**
     * 判断用户是否已经登录
     * @param username 用户名 token token值
     */
    @GetMapping("/check-login")
    public Result<Boolean>checkLogin(@RequestParam String username,@RequestParam String token){
        return Results.success(userService.checkLogin(username,token));
    }
    @DeleteMapping("/logout")
    public Result<Boolean>logOut(@RequestParam String username,@RequestParam String token){
        return Results.success(userService.logOut(username,token));
    }
}
